package com.lyxs916.appbaselibrary.core.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lyxs916.appbaselibrary.core.activity.BaseActivity;
import com.lyxs916.appbaselibrary.core.iview.IActivityAndFragment;
import com.lyxs916.utilslibrary.ui.UiUtils;

import butterknife.ButterKnife;
import rx.Subscription;
import rx.subscriptions.CompositeSubscription;

/**
 * 说明:
 * 作者： 杨阳; 创建于：  2017-06-14  15:14
 */
public abstract class BaseFrament extends Fragment implements IActivityAndFragment {

    /**
     * 说明:请求code;
     * 注：%60000是因为请求的code最大不超过65535
     * 作者： 杨阳; 创建于：  2017-06-01  17:34
     */
    public int REQUEST_CODE = Math.abs(this.getClass().getSimpleName().hashCode() % 60000);
    /**
     * 订阅的集合，防止内存泄漏
     */
    private CompositeSubscription mCompositeSubscription;
    /**
     * 宿主activity
     */
    protected BaseActivity mActivity;
    /**
     * 布局
     */
    protected View view;
    /**
     * 是否是回收利用的Fragment
     */
    protected boolean isRecycle = false;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = (BaseActivity) getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            isRecycle = false;
            view = UiUtils.getInflaterView(mActivity, getContentView());
            ButterKnife.bind(this, view);
            UiUtils.applyFont(view);

        } else {
            isRecycle = true;
        }
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (!isRecycle) {
            initView();
        }
    }

    /**
     * 说明:获取布局id
     * 作者： 杨阳; 创建于：  2017-06-14  15:19
     */
    public abstract int getContentView();

    /**
     * 说明:初始化view
     * 作者： 杨阳; 创建于：  2017-06-14  15:18
     */
    public abstract void initView();


    @Override
    public void onDetach() {
        super.onDetach();
        /**
         * 销毁网络访问的订阅
         */
        if (this.mCompositeSubscription != null) {
            this.mCompositeSubscription.unsubscribe();
        }
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        if (this.mCompositeSubscription != null) {
            this.mCompositeSubscription.unsubscribe();
        }
    }

    @Override
    public CompositeSubscription getCompositeSubscription() {
        if (this.mCompositeSubscription == null) {
            this.mCompositeSubscription = new CompositeSubscription();
        }
        return this.mCompositeSubscription;
    }

    @Override
    public void addSubscription(Subscription s) {
        if (this.mCompositeSubscription == null) {
            this.mCompositeSubscription = new CompositeSubscription();
        }
        this.mCompositeSubscription.add(s);
    }

    public BaseActivity getBaseActivity() {
        return mActivity;
    }


}
